/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.utils;

import haxe.Timer;

private typedef Section = {
    var time : Float;
    var name : String;
}

class Timing {
    
    private static var sections : Array<Section>;
    private static var lastTime : Float;
    
    public static function init() {
        #if timing
        sections = new Array();
        lastTime = Timer.stamp();
        #end
    }
    
    public static function record(name : String) {
        #if timing
        var now = Timer.stamp();
        sections.push( {time : now - lastTime, name : name} );
        lastTime = now;
        #end
    }
    
    public static function print() {
        #if timing
        sections.sort(function (sec1 : Section, sec2 : Section) : Int {
            return if (sec1.time < sec2.time) 1 else -1;
        });
        
        for (section in sections) {
            trace(section.name + " - " + section.time + " seconds");
        }
        #end
    }
    
    public static function clear() {
        #if timing
        lastTime = Timer.stamp();
        #end
    }
    
}